package com.wx.mall.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.mall.common.exception.BusinessException;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.goods.entity.Spec;
import com.wx.mall.goods.service.ISpecService;
import com.wx.mall.goods.vo.req.PageReqVO;
import com.wx.mall.goods.vo.resp.PageVO;
import com.wx.mall.goods.vo.resp.ParaSpecRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 规格表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/spec")
@Api(tags = "规格模块相关接口")
public class SpecController {
    @Autowired
    private ISpecService specService;

    @PostMapping("/specs")
    @ApiOperation("分页获取规格数据")
    public DataResult pageInfo(@Valid PageReqVO vo) {
        PageVO<ParaSpecRespVO> pageVO = specService.pageInfo(vo);
        return DataResult.success(pageVO);
    }

    @DeleteMapping("/spec/{id}")
    @ApiOperation("根据id删除数据")
    public DataResult deleteById(@ApiParam("id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        boolean b = specService.removeById(id);
        if (!b) {
            throw new BusinessException(8001, "删除失败");
        }
        return DataResult.success();
    }

    @GetMapping("/spec/{id}")
    @ApiOperation("根据id查询数据")
    public DataResult findById(@ApiParam("id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        Spec byId = specService.getById(id);
        if (byId == null) {
            ApiAssert.fail("查询失败");
        }
        return DataResult.success(byId);
    }

    @GetMapping("/spec/temp/{id}")
    @ApiOperation("根据模板id查询数据")
    public DataResult findByTemplateId(@ApiParam("id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        List<Spec> specs = specService.list(new QueryWrapper<Spec>().eq("template_id", id));
//        if (specs.size() == 0) {
//            ApiAssert.fail("查询失败");
//        }
        return DataResult.success(specs);
    }

    @PutMapping("/spec")
    @ApiOperation("修改规格接口")
    public DataResult updateById(@Valid Spec spec) {
        System.out.println("参数是:" + spec.getId());
        boolean b = specService.updateById(spec);
        if (!b) {
            throw new BusinessException(8002, "修改失败");
        }
        return DataResult.success();
    }

    @PostMapping("/spec")
    @ApiOperation("新增接口")
    public DataResult add(@Valid Spec spec) {
        System.out.println(spec.getId());
        boolean save = specService.save(spec);
        if (!save) {
            throw new BusinessException(8003, "新增失败");
        }
        return DataResult.success();
    }

    @DeleteMapping("/specs/{ids}")
    @ApiOperation("批量删除规格接口")
    public DataResult removes(@ApiParam("ids") @PathVariable("ids") @NotBlank(message = "id不能为空") String ids) {
        // 接收包含stuId的字符串，并将它分割成字符串数组
        String[] idsList = ids.split(",");
        // 将字符串数组转为List<Intger> 类型
        List<Integer> LString = new ArrayList<Integer>();
        for (String str : idsList) {
            LString.add(new Integer(str));
        }
        boolean b = specService.removeByIds(LString);
        if (!b) {
            throw new BusinessException(8004, "批量删除失败");
        }
        return DataResult.success();
    }
}

